<template>
  <div class="page-container">
    <el-card>
      <div slot="header">
        <span>订单管理</span>
      </div>

      <!-- 工具栏 -->
      <div class="toolbar">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">创建订单</el-button>
      </div>

      <!-- 表格 -->
      <el-table :data="tableData" border stripe v-loading="loading">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="orderNo" label="订单号" width="200"></el-table-column>
        <el-table-column prop="userId" label="用户ID" width="100"></el-table-column>
        <el-table-column prop="productId" label="商品ID" width="100"></el-table-column>
        <el-table-column prop="quantity" label="数量" width="100"></el-table-column>
        <el-table-column prop="totalAmount" label="总金额" width="120"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleView(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="10"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 创建订单对话框 -->
    <el-dialog title="创建订单" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" ref="form" label-width="100px">
        <el-form-item label="用户ID">
          <el-input v-model.number="form.userId" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品ID">
          <el-input v-model.number="form.productId" type="number"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model.number="form.quantity" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderList, createOrder } from '@/api/order';

export default {
  name: 'OrderIndex',
  data() {
    return {
      loading: false,
      tableData: [],
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      form: {
        userId: null,
        productId: null,
        quantity: 1
      }
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.loading = true;
      // 模拟数据
      setTimeout(() => {
        this.tableData = [
          {
            id: 1,
            orderNo: 'ORD' + Date.now(),
            userId: 1,
            productId: 1,
            quantity: 2,
            totalAmount: 200.00,
            status: 0,
            createTime: new Date().toLocaleString()
          }
        ];
        this.total = 1;
        this.loading = false;
      }, 500);
    },
    handleAdd() {
      this.form = { userId: null, productId: null, quantity: 1 };
      this.dialogVisible = true;
    },
    handleView(row) {
      this.$alert(JSON.stringify(row, null, 2), '订单详情', {
        confirmButtonText: '确定'
      });
    },
    handleSubmit() {
      createOrder(this.form).then(() => {
        this.$message.success('创建成功');
        this.dialogVisible = false;
        this.loadData();
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.loadData();
    },
    getStatusType(status) {
      const types = ['info', 'warning', 'success', 'danger'];
      return types[status] || 'info';
    },
    getStatusText(status) {
      const texts = ['待支付', '待发货', '已完成', '已取消'];
      return texts[status] || '未知';
    }
  }
};
</script>
